<?php
    header("content-type:text/html;charset=utf-8");
    $pdo = new PDO("mysql:host=127.0.0.1;dbname=exam","root","root");
    $city = $_POST['city'];
//    print_r($city);die;
    $selectsql = "select * from wea where city = '$city'";
    $res = $pdo->query($selectsql)->fetchAll(PDO::FETCH_ASSOC);

    if(!$res){
        $url = "http://www.wea.com/ser.php?city=$city";
        $data = file_get_contents($url);
//        print_r($data);die;
        $data = json_decode($data,true);
//        print_r($data);die;
        $res = $data['result'];
//        print_r($res);die;

        foreach ($res as $value){
            $week = $value['week'];
            $city = $value['citynm'];
            $temp_high = $value['temp_high'];
            $temp_low = $value['temp_low'];
            $temperature = $value['temperature'];
            $sql = "insert into wea value('','$week','$city','$temp_high','$temp_low','$temperature')";
//            print_r($sql);die;
            $pdo->exec($sql);
           }
    }
echo json_encode($res);

